
<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="路径">
        <el-input v-model="searchParam.path" />
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="searchParam.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRowVisible()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="path"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="path" label="路径" />
      <el-table-column prop="code" label="名称" />
      <el-table-column prop="meta.title" label="标题" />
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="editRowVisible(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="delRowHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogFormVisible" title="">
      <el-form :model="form">
        <el-form-item label="父菜单" :label-width="formLabelWidth">
          <el-cascader
            v-model="form.parent"
            :options="options"
            :props="{ emitPath: false, checkStrictly: true }"
            clearable
          />
        </el-form-item>
        <el-form-item label="路径" :label-width="formLabelWidth">
          <el-input v-model="form.path" autocomplete="off" />
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRowHandle">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList, getListForSelect, addRow, delRow, editRow } from '@/api/system/menu'

export default {
  data() {
    return {
      tableData: [],
      listLoading: true,
      searchParam: {
        path: '',
        name: ''
      },
      dialogFormVisible: false,
      form: {
        path: '',
        name: '',
        code: '',
        parent: ''
      },
      formLabelWidth: '120px',
      'options': []
    }
  },
  created() {
    this.fetchData()
    this.fetchSelectData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.tableData = response.data
        this.listLoading = false
      })
    },
    handleCurrentChange(page) {
      this.tablePage = page
      console.log(page, this.tablePageSize)
      this.fetchData()
    },
    handleSizeChange(pagesize) {
      this.tablePageSize = pagesize
      this.fetchData()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    fetchSelectData() {
      getListForSelect().then(response => {
        this.options = response.data
      })
    },
    addRowVisible() {
      // 解决form缓存问题
      this.form = {}
      this.dialogFormVisible = true
    },
    editRowVisible(row) {
      this.fetchSelectData()
      this.form = {
        path: row.path,
        name: row.meta.title,
        code: row.code,
        parent: row.parent,
        id: row.id
      }
      this.dialogFormVisible = true
    },
    delRowHandle(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行调用删除接口
          delRow({ id: row.id }).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRowHandle() {
      this.dialogFormVisible = false
      console.log(this.form.id)
      if (this.form.id !== undefined) {
        editRow(this.form.id, this.form).then(response => {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.fetchData()
        })
      } else {
        addRow(this.form).then(response => {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.fetchData()
        })
      }
    }
  }
}
</script>

<style>

</style>
